/**
* \file Flotte.cpp
* \brief Ensemble des methodes de la classe Flotte
* \author FEKI Mehdi && RIPOCHE Emilien
* \version 1.0
* \date 04/12/2011
*/

#include <iostream>
#include "Flotte.h"
#include "FabriqueVaisseauMere.h"
#include "FabriqueBattlecruiser.h"
#include "FabriqueCapsule.h"
#include "VaisseauMere.h"

Flotte::Flotte(CouleurFlotte c) 
	: _couleurFlotte(c), _viesRestantes(2), _x(-1), _y(-1) {
	FabriqueVaisseau* _fab = new FabriqueVaisseauMere();
	_vaisseau = _fab->creerVaisseau();
};

unsigned int Flotte::getNbVaisseaux() const {
	/*switch(_vaisseau->type()) {
	case(Vaisseau::VaisseauMere):
		return 3;
		break;
	case(Vaisseau::Battlecruiser):
		return 2;
		break;
	case(Vaisseau::Capsule):
		return 1;
		break;
	default:
		return 0;
	}*/
	return this->_viesRestantes+1;
}


Vaisseau::TypeVaisseau Flotte::downgradeVaisseau() {
	cout << "Flotte::downgradeVaisseau()" << endl;
	cout << "Couleur = " << this->getCouleurFlotte() << endl;
	FabriqueVaisseau* fab;
	int posX = _vaisseau->getX();
	int posY = _vaisseau->getY();
	switch (_vaisseau->type()) {
	case(Vaisseau::VaisseauMere) :
		fab = new FabriqueBattlecruiser();
		if(_vaisseau->hasArtefact()){
			cout << "AHHHHHHHHHHHHHHHH" << endl;
			_vaisseau = fab->creerVaisseau();
			_vaisseau->setHasArtefact(true);
		}	
		else {
			_vaisseau = fab->creerVaisseau();
		}
		_vaisseau->setX(posX);
		_vaisseau->setY(posY);
		return Vaisseau::Battlecruiser;
		break;
	case(Vaisseau::Battlecruiser) :
		fab = new FabriqueCapsule();	
		_vaisseau = fab->creerVaisseau();
		_vaisseau->setX(posX);
		_vaisseau->setY(posY);
		return Vaisseau::Capsule;
		break;
	case(Vaisseau::Capsule) :
		if (_viesRestantes < 1) {
			delete _vaisseau;
			delete fab;
			return (Vaisseau::TypeVaisseau)NULL;
		}
		--_viesRestantes;
		fab = new FabriqueVaisseauMere();
		_vaisseau = fab->creerVaisseau();
		_vaisseau->setX(this->_x);
		_vaisseau->setY(this->_y);
		return Vaisseau::VaisseauMere;
		break;
	default :
		return (Vaisseau::TypeVaisseau)NULL;
	}
};

Flotte::CouleurFlotte Flotte::stringToCouleurFlotte(string cf) {
	if (cf == "Bleu")
		return Flotte::Bleu;
	else if (cf == "Jaune")
		return Flotte::Jaune;
	else if (cf == "Rouge") 
		return Flotte::Rouge;
	else
		return Flotte::Vert;
};

void Flotte::setX(int x) {
	this->_x = x;
	this->_vaisseau->setX(x);
}

void Flotte::setY(int y) {
	this->_y = y;
	this->_vaisseau->setY(y);
}

int Flotte::getX() const {
	return this->_x;
}

int Flotte::getY() const {
	return this->_y;
}

bool Flotte::hasVaisseau(int x, int y) {
	cout << "Flotte::HasVaisseau(" << x  << "," << y << ")" << endl;
	if(this->_vaisseau->getX() == x && this->_vaisseau->getY() == y) {
		return true;
	}
	return false;
}

/*
int main() {
	Flotte* f = new Flotte(Flotte::Bleu);
	Vaisseau* v = f->getVaisseau();
	cout << "Vaisseau de type " << f->getVaisseau()->type() << " aux coordonnees " 
		 << v->getX() << "," << v->getY() 
		 << (v->hasArtefact() ? " avec artefact" : " sans artefact") << endl
		 << " --> Downgrade " << (f->downgradeVaisseau() ? "ok" : "failed") << endl;
	v = f->getVaisseau();
	cout << "Vaisseau de type " << f->getVaisseau()->type() << " aux coordonnees " 
		 << v->getX() << "," << v->getY() 
		 << (v->hasArtefact() ? " avec artefact" : " sans artefact") << endl
		 << " --> Downgrade " << (f->downgradeVaisseau() ? "ok" : "failed") << endl;
	v = f->getVaisseau();
	cout << "Vaisseau de type " << f->getVaisseau()->type() << " aux coordonnees " 
		 << v->getX() << "," << v->getY() 
		 << (v->hasArtefact() ? " avec artefact" : " sans artefact") << endl
		 << " --> Downgrade " << (f->downgradeVaisseau() ? "ok" : "failed") << endl;
	v = f->getVaisseau();
	cout << "Vaisseau de type " << f->getVaisseau()->type() << " aux coordonnees " 
		 << v->getX() << "," << v->getY() 
		 << (v->hasArtefact() ? " avec artefact" : " sans artefact") << endl
		 << " --> Downgrade " << (f->downgradeVaisseau() ? "ok" : "failed") << endl;
	v = f->getVaisseau();
	cout << "Vaisseau de type " << f->getVaisseau()->type() << " aux coordonnees " 
		 << v->getX() << "," << v->getY() 
		 << (v->hasArtefact() ? " avec artefact" : " sans artefact") << endl
		 << " --> Downgrade " << (f->downgradeVaisseau() ? "ok" : "failed") << endl;
	v = f->getVaisseau();
	cout << "Vaisseau de type " << f->getVaisseau()->type() << " aux coordonnees " 
		 << v->getX() << "," << v->getY() 
		 << (v->hasArtefact() ? " avec artefact" : " sans artefact") << endl
		 << " --> Downgrade " << (f->downgradeVaisseau() ? "ok" : "failed") << endl;
	v = f->getVaisseau();
	cout << "Vaisseau de type " << f->getVaisseau()->type() << " aux coordonnees " 
		 << v->getX() << "," << v->getY() 
		 << (v->hasArtefact() ? " avec artefact" : " sans artefact") << endl
		 << " --> Downgrade " << (f->downgradeVaisseau() ? "ok" : "failed") << endl;
	v = f->getVaisseau();
	cout << "Vaisseau de type " << f->getVaisseau()->type() << " aux coordonnees " 
		 << v->getX() << "," << v->getY() 
		 << (v->hasArtefact() ? " avec artefact" : " sans artefact") << endl
		 << " --> Downgrade " << (f->downgradeVaisseau() ? "ok" : "failed") << endl;
	v = f->getVaisseau();
	cout << "Vaisseau de type " << f->getVaisseau()->type() << " aux coordonnees " 
		 << v->getX() << "," << v->getY() 
		 << (v->hasArtefact() ? " avec artefact" : " sans artefact") << endl
		 << " --> Downgrade " << (f->downgradeVaisseau() ? "ok" : "failed") << endl;

	system("PAUSE");
}
*/